package com.ruoyi.Api;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.countrybus.domain.SysCustomer;
import com.ruoyi.project.countrybus.service.ISysCustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api("乘客端API")
@RestController
@RequestMapping("/api/customer")
public class CustomerApi extends BaseController
{

    @Autowired
    private ISysCustomerService sysCustomerService;

    @ApiOperation("乘客端登陆验证")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "customerName", value = "乘客账号", required = true, dataType = "String", paramType = "path", dataTypeClass = String.class),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String", paramType = "path", dataTypeClass = String.class)
    })
    @GetMapping("/customerlogin/{customerName}/{password}")
    public AjaxResult customerlogin(@PathVariable("customerName") String customerName, @PathVariable("password") String password)
    {

        SysCustomer sysCustomer = sysCustomerService.selectSysCustomerByCustomerName(customerName);
        String pass = sysCustomer.getPassword();

        if (StringUtils.equals(pass,password))
        {
            return AjaxResult.success(sysCustomer);
//            return AjaxResult.success("登陆成功");
        }
        else
        {
            return error("用户不存在");
        }
    }


    @ApiOperation("乘客端找回密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phonenumber", value = "乘客手机号", required = true, dataType = "String", paramType = "path", dataTypeClass = String.class),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String", paramType = "path", dataTypeClass = String.class)
    })
    @GetMapping("/findpassword/{phonenumber}/{password}")
    public AjaxResult customerchangepassword(@PathVariable("phonenumber") String phonenumber,@PathVariable("password") String password)
    {

        SysCustomer sysCustomer = sysCustomerService.selectSysCustomerByCustomerPhonenumber(phonenumber);
        sysCustomer.setPassword(password);

        return toAjax(sysCustomerService.updateSysCustomer(sysCustomer));
    }


    /**
     * 查询乘客信息列表
     */
    @GetMapping("/list")
    @ApiOperation("获取乘客列表")
    public TableDataInfo list(SysCustomer sysCustomer)
    {
        startPage();
        List<SysCustomer> list = sysCustomerService.selectSysCustomerList(sysCustomer);
        return getDataTable(list);
    }


    /**
     * 获取乘客信息详细信息
     */
    @ApiOperation("获取乘客详情信息")
    @ApiImplicitParam(name = "customerId", value = "乘客ID", required = true, dataType = "long", paramType = "path", dataTypeClass = Long.class)
    @GetMapping(value = "/{customerId}")
    public AjaxResult getInfo(@PathVariable("customerId") Long customerId)
    {
        return AjaxResult.success(sysCustomerService.selectSysCustomerByCustomerId(customerId));
    }


    /**
     * 获取乘客信息详细信息，通过账号
     */
    @ApiOperation("通过账号获取乘客详情信息")
    @ApiImplicitParam(name = "customerName", value = "乘客账号", required = true, dataType = "string", paramType = "path", dataTypeClass = String.class)
    @GetMapping(value = "/name/{customerName}")
    public AjaxResult getInfoByName(@PathVariable("customerName") String customerName)
    {
        return AjaxResult.success(sysCustomerService.selectSysCustomerByCustomerName(customerName));
    }

    /**
     * 新增乘客信息
     */
    @ApiOperation("新增乘客信息")
    @PostMapping
    public AjaxResult add(@RequestBody SysCustomer sysCustomer)
    {
        return toAjax(sysCustomerService.insertSysCustomer(sysCustomer));
    }

    /**
     * 修改乘客信息
     */
    @ApiOperation("修改乘客信息")
    @PutMapping
    public AjaxResult edit(@RequestBody SysCustomer sysCustomer)
    {
        return toAjax(sysCustomerService.updateSysCustomer(sysCustomer));
    }

    /**
     * 删除乘客信息
     */
    @ApiOperation("删除乘客信息")
    @DeleteMapping("/{customerIds}")
    public AjaxResult remove(@PathVariable Long[] customerIds)
    {
        return toAjax(sysCustomerService.deleteSysCustomerByCustomerIds(customerIds));
    }


}
